﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinStudent
{
    public partial class FrmAddSC : Form
    {
        public FrmAddSC()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化课程号列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAddGrade_Load(object sender, EventArgs e)
        {
            InitCnoList();
        }

        private void InitCnoList()
        {
            string sql = "select Cno from Course order by Cno ";
            DataTable dataTableCno = SqlHelper.GetDataTable(sql);

            cboCno.DataSource = dataTableCno;
            cboCno.DisplayMember = "Cno";//显示的内容
            cboCno.ValueMember = "Cno";//值

            cboCno.SelectedIndex = 0;
        }

        /// <summary>
        /// 添加选课成绩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //信息获取
            string sno = txtSno.Text.Trim();
            string cno = (string)cboCno.SelectedValue;
            //判断成绩是否有效
            try
            {
                int.Parse(txtGrade.Text.Trim());
            }
            catch (Exception)
            {
                MessageBox.Show("成绩无效", "选课信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            int grade = int.Parse(txtGrade.Text.Trim());

            //判断学号是否为空
            if (string.IsNullOrEmpty(sno))
            {
                MessageBox.Show("学号不能为空", "添加学号提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //判断该条信息是否已存在----与数据库进行交互
            {
                //判断学号是否存在管理系统中
                string sqlSno = "select count(*) from Student where Sno=@Sno";
                SqlParameter[] parasSno =
{
                    new SqlParameter("@Sno",sno)
                };
                object oSno = SqlHelper.ExecuteScalar(sqlSno, parasSno);
                if (oSno == null || oSno == DBNull.Value || ((int)oSno) == 0)
                {
                    MessageBox.Show("学号不存在,请先录入学生信息", "学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                string sql = "select count(*) from SC where Sno=@Sno and Cno=@Cno ";
                SqlParameter[] paras =
                {
                    new SqlParameter("@Sno",sno),
                    new SqlParameter("@Cno",cno)
                };
                object o = SqlHelper.ExecuteScalar(sql, paras);
                if (o == null||o==DBNull.Value||((int)o)==0)
                {
                    //添加操作
                    string sqlAdd = "insert into SC (Sno,Cno,Grade) values (@Sno,@Cno,@Grade)";
                    SqlParameter[] parasAdd =
                    {
                        new SqlParameter("@Sno",sno),
                        new SqlParameter("@Cno",cno),
                        new SqlParameter("@Grade",grade)
                    };
                    //执行并返回值
                    int count = SqlHelper.ExecutNonQuery(sqlAdd, parasAdd);
                    if (count>0)
                    {
                        MessageBox.Show("选课信息添加成功", "选课信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("选课信息添加失败", "选课信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("学生选课信息已存在", "选课信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
